iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
0
自我挑戰組

從 RedHat OpenShift 社群版 OKD 看 Kubernetes系列 第 15

[偷一下未來進度]Day 15 :Kubernets有狀態應用的管理

  • 分享至 

  • xImage
  •  

容器應用可以說是當紅炸子雞,而 Kubernetes 是目前最為流行的容器管理系統,用於管理容器化應用程式的生命週期與設定的相關工作。在 Kubernetes 的系統架構中一切都可視為資源,並提供多種預設的抽象的資源類型,如 Pod、Deployment、Service、Volume 等一系列抽象的資源,這些內建的抽象的資源能夠滿足大多數日常應用部署和管理的需求。但是在一些特殊的需求場景下( 例如前幾天我們部署的 EFK Log 日誌系統), Kubernetes 這些現有的資源型態類型滿足不了這些需求(部署上變得複雜難以管理)。

例如在官方網站的範例中就向我們展示,維運人員可以透過 Kubernetes 預設的 StatefulSet 資源部署一個高可以用的( High-Availability , HA )MySQL。

application/mysql/mysql-statefulset.yaml 

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
      - name: init-mysql
        image: mysql:5.7
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Generate mysql server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # Add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # Copy appropriate conf.d files from config-map to emptyDir.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d/
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        image: gcr.io/google-samples/xtrabackup:1.0
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # Skip the clone on master (ordinal index 0).
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # Clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # Prepare the backup.
          xtrabackup --prepare --target-dir=/var/lib/mysql
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 500m
            memory: 1Gi
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        image: gcr.io/google-samples/xtrabackup:1.0
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # Determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # XtraBackup already generated a partial "CHANGE MASTER TO" query
            # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
            # Ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # We're cloning directly from master. Parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm -f xtrabackup_binlog_info xtrabackup_slave_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi

          # Check if we need to complete a clone by starting replication.
          if [[ -f change_master_to.sql.in ]]; then
            echo "Waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done

            echo "Initializing replication from clone position"
            mysql -h 127.0.0.1 \
                  -e "$(<change_master_to.sql.in), \
                          MASTER_HOST='mysql-0.mysql', \
                          MASTER_USER='root', \
                          MASTER_PASSWORD='', \
                          MASTER_CONNECT_RETRY=10; \
                        START SLAVE;" || exit 1
            # In case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
          fi

          # Start a server to send backups when requested by peers.
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 10Gi

但是我們可以從這一個 yaml 的設定過程中發現設定一個 High-Availability MySQL 相對複雜。維運人員必須熟悉 Kubernetes 各式各樣的抽象資源,同時也要了解 MySQL 操作的細節,此外維運人員還要維護一組複雜的管理腳本。
Kubernetes 針對這種情況在 1.7 版本中增加了自定義資源類型 (Custom Resource Definition; CRD ) ,透過自定義資源所提供的二次開發的能力來擴展 Kubernetes API ,開發人員可以通過 CRD 可以向 Kubernetes API 增加新資源類型,並提供比預設的 Deployment 或 StatefulSet .....等更適合特定領域的部署與管理模式,而不需要修改 Kubernetes 的程式碼或特別建立自己的 API Server,該功能大大提高了 Kubernetes 的擴展能力。

apiVersion: mysql.oracle.com/v1alpha1
kind: Cluster
metadata:
  name: mysql
spec:
  members: 3

可以從上面這個 MySQL CRD 設定 MySQL Cluster 的範例中看到, Yaml 樣板變得十分簡單,但其實這個 MySQL CRD 只定義了 MySQL Cluster 的設定檔案,沒有人來真的實作這一個 MySQL CRD (就如同你寫了一大堆設定檔,沒有人來實做這些設定依一樣),關於這個問題其實 Kubernetes 社群提供非常多的解決方法,明天我將來介紹有些方法可以來管理 CRD 。


上一篇
[偷一下未來進度]Day 14 :Kubernetes 純手動部署與設定 EFK (4/4)
下一篇
[偷一下未來進度]Day 16 :單節點 OKD 安裝介紹
系列文
從 RedHat OpenShift 社群版 OKD 看 Kubernetes17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言